<?php
    include_once('Configs/database.php');
    include_once('ThoiKhoaBieu.php');
    class NhomThucHanh extends database{
		private $manhomth;
		private $kihieu;
        private $malophp;
      	private $maloai;
		private $thoigianapdung;
        private $maso;
        
        private $siso;
       
        public function setMaNhomTH($manhomth){
            $this->manhomth = $manhomth;
        }
        public function getMaNhomTH(){
            return $this->manhomth;
        }
		 public function setKiHieu($kihieu){
            $this->kihieu =  $kihieu;
        }
        public function getKiHieu(){
            return $this->kihieu;
        }
        public function setMaLopHP($malophp){
            $this->malophp = $malophp;
        }
        public function getMaLopHP(){
            return $this->malophp;
        }
        
        public function setMaSo($maso){
            $this->maso = $maso;
        }
        public function getMaSo(){
            return $this->maso;
        }
        
       
        
        
        public function setSiSo($siso){
            $this->siso = $siso;
        }
        public function getSiSo(){
            return $this->siso;
        }
        
       	public function setMaLoai($maloai){
			$this->maloai = $maloai;
		}
        public function getMaLoai(){
			return $this->maloai;
		}   
		public function setThoiGianApDung($thoigianapdung){
			$this->thoigianapdung = $thoigianapdung;
		}
        public function getThoiGianApDung(){
			return $this->thoigianapdung;
		}             
        public function getHeSo(){
            $query = "select heso from NhomThucHanh a, LoaiNhomTH b ";
            $query .= "where manhomth='".$this->getMaNhomTH()."' and a.maloai=b.maloai and a.thoigianapdung=b.thoigianapdung";
			$this->setQuery($query);
            $rusult = $this->fetchAll();
            $rows = mysql_fetch_array($rusult);
			return $rows['heso'];
		}
		 public function themNhomThucHanh(){
            mysql_query("START TRANSACTION");
            $query = "insert into NhomThucHanh(manhomth,kihieu,malophp,maloai,thoigianapdung,siso,maso) values('".$this->getMaNhomTH()."','".$this->getKiHieu()."', ";
            $query .= "'".$this->getMaLopHP()."',".$this->getMaLoai().",'".$this->getThoiGianApDung()."',".$this->getSiSo().",'".$this->getMaSo()."')";
			
            $this->setQuery($query);
			//echo $this->getQuery();
            $i = 0;
            if ($this->executeQuery() >0 ){
                $this->setQuery("insert into NhatKiNhomTH(manhomth) values ('".$this->getMaNhomTH()."')");
                
                $i = $this->executeQuery();
                if ($i > 0){
                    mysql_query("COMMIT"); 
                }
                                                                                                                                                                                                                  
            }
            return $i;
            //echo $this->getQuery();
            
        }
		public function xoaNhomThucHanh(){
			mysql_query("START TRANSACTION");
            $this->setQuery("delete NhatKiNhomTH where manhomth='".$this->getMaNhomTH()."'");
			//echo $this->getQuery();
            $i = 0;
            if ($this->executeQuery() >0 ){
               $this->setQuery("delete NhomThucHanh where manhomth='".$this->getMaNhomTH()."'");
                
                $i = $this->executeQuery();
                if ($i > 0){
                    mysql_query("COMMIT"); 
                }
                                                                                                                                                                                                                  
            }
            return $i;
           
        }
        /*public function soTietTHQuyChuanTheoKH(){
            $query = "select a.malophp, round(sum(sotietthuchanh *heso),0) as sotietthquychuan ";
            $query .= "from LopHocPhan a, NhomThucHanh b,LoaiNhomTH c, HocPhan d ";

            $query .= "where a.malophp='".$this->getMaLopHP()."' and a.malophp = b.malophp and b.maloai = c.maloai and a.mahp = d.mahp ";
            $query .= "group by a.malophp having count(*)>0";
            $this->setQuery($query);
            $result = $this->fetchAll();
            $rows = mysql_fetch_array($result);
            $sotietthquychuan = 0;
            if ($rows['sotietthquychuan'] > 0) $sotietthquychuan = $rows['sotietthquychuan'];
            
            
            return $sotietthquychuan;
            
        }*/
        public function tongSoTietDaDay(){
            $query = "select manhomth,sum(sotiet) as tongsotiet from NhatKiChiTietNhomTH where manhomth='".$this->getMaNhomTH()."' group by manhomth";
            $this->setQuery($query);
            $result = $this->fetchAll();
            $rows = mysql_fetch_array($result);
            
            if (trim($rows['tongsotiet']) <= 0)
                $tongsotiet = 0;
            else $tongsotiet = $rows['tongsotiet'];
            
            return $tongsotiet;
        }
		public function suaNhomThucHanh(){
            
            $query = "update NhomThucHanh set  maloai=".$this->getMaLoai().",thoigianapdung='".$this->getThoiGianApDung()."',maso='".$this->getMaSo()."', ";
            $query .= "siso=".$this->getSiSo()." where manhomth='".$this->getMaNhomTH()."'";
            $this->setQuery($query);
            //echo $this->getQuery();
            return $this->executeQuery();
            
            
        }        
        public function soTietDayDungKeHoach(){
            $sotietdungkehoach = 0;
            $query = "select a.manhomth, a.tuan, sum(a.sotiet) as sotietdungkhmoituan ";
            $query .= "from NhatKiChiTietNhomTH a, NhatKiNhomTH b, NhomThucHanh c, LichThucHanh d ";
            $query .= "where a.manhomth='".$this->getMaNhomTH()."' and a.manhomth= b.manhomth and b.manhomth = c.manhomth and c.manhomth = d.manhomth and a.thu = d.thu ";
            $query .= "group by a.manhomth, a.tuan";
             $this->setQuery($query);
             //echo $this->getQuery();
            $result = $this->fetchAll();
            
            while ($rows = mysql_fetch_array($result)){
                if (trim($rows['sotietdungkhmoituan']) <= 0)
                    $sotiet = 0;
                else $sotiet = $rows['sotietdungkhmoituan'];
                $sotietdungkehoach = $sotietdungkehoach + $sotiet;
            }
            return $sotietdungkehoach;
        }
        
    }
?>